let sourceData = [{
    product: "手机",
    region: "华东",
    sale: [120, 100, 140, 160, 180, 185, 190, 210, 230, 245, 255, 270]
}, {
    product: "手机",
    region: "华北",
    sale: [80, 70, 90, 110, 130, 145, 150, 160, 170, 185, 190, 200]
}, {
    product: "手机",
    region: "华南",
    sale: [220, 200, 240, 250, 260, 270, 280, 295, 310, 335, 355, 380]
}, {
    product: "笔记本",
    region: "华东",
    sale: [50, 60, 80, 110, 30, 20, 70, 30, 420, 30, 20, 20]
}, {
    product: "笔记本",
    region: "华北",
    sale: [30, 35, 50, 70, 20, 15, 30, 50, 710, 130, 20, 20]
}, {
    product: "笔记本",
    region: "华南",
    sale: [80, 120, 130, 140, 70, 75, 120, 90, 550, 120, 110, 100]
}, {
    product: "智能音箱",
    region: "华东",
    sale: [10, 30, 4, 5, 6, 5, 4, 5, 6, 5, 5, 25]
}, {
    product: "智能音箱",
    region: "华北",
    sale: [15, 50, 15, 15, 12, 11, 11, 12, 12, 14, 12, 40]
}, {
    product: "智能音箱",
    region: "华南",
    sale: [10, 40, 10, 6, 5, 6, 8, 6, 6, 6, 7, 26]
}]
let area = document.querySelector("#area")
let products = document.querySelector("#products")
let table = document.querySelector("#table-wrapper")

if (localStorage.getItem("sourceData")) {
    sourceData = JSON.parse(localStorage.getItem("sourceData"))
}

function getCheckbox(data) {
    let areaArr = []
    let productArr = []
    for (let i = 0; i < data.length; i++) {
        if (areaArr.indexOf(data[i].region) == -1) {
            areaArr.push(data[i].region)
        } if (productArr.indexOf(data[i].product) == -1) {
            productArr.push(data[i].product)
        }
    }
    return [areaArr, productArr]
}

function createCheckbox() {
    let areaArr = getCheckbox(sourceData)[0]
    areaArr.unshift("全选")
    let productArr = getCheckbox(sourceData)[1]
    productArr.unshift("全选")
    area.innerHTML = ""
    products.innerHTML = ""
    for (let j = 0; j < areaArr.length; j++) {
        let label = document.createElement("label")
        let input = document.createElement("input")
        label.innerText = areaArr[j]
        input.type = "checkbox"
        input.name = "area"
        j == 0 ? input.checked = "checked" : ""
        input.value = areaArr[j]
        label.insertBefore(input, label.childNodes[0])
        area.appendChild(label)
    }
    for (let k = 0; k < productArr.length; k++) {
        let label = document.createElement("label")
        let input = document.createElement("input")
        label.innerText = productArr[k]
        input.type = "checkbox"
        input.name = "products"
        k == 0 ? input.checked = "checked" : ""
        input.value = productArr[k]
        label.insertBefore(input, label.childNodes[0])
        products.appendChild(label)
    }
}
createCheckbox()
